Xendri.link = Class({
	clean: false,
	url: "",
	__parsed:{},
	initialize: function(link) {
		this.url = link;
		this.__parsed = this.__parseLongLink(link);
	}, 
	__parseLongLink: function(url) {
		url = String(url);
		var tmp = url.split("/");
		var res = {};
		var temp;
		var nodesArray = [];
		var nodesByTarget = [];
		for(var x = 0; x < tmp.length; x++) {
			temp = this.__parseShortLink(tmp[x]);
			nodesArray[x] = temp;
			if (!nodesByTarget[temp.target]) nodesByTarget[temp.target] = [];
			nodesByTarget[temp.target].push(x);
		}
		var result = [];
		for(var x=tmp.length-1; x>-1; x--) {
			var t = nodesArray[x].target;
			if (nodesByTarget[t].length > 1) {
				x = nodesByTarget[t][0];
				var act = nodesByTarget[t].pop();
			} else {
				var act = x;
			}
			result.unshift(nodesArray[act]);
		}
		return result;
	},
	__parseShortLink: function(link) {
		if (!link) return false;
		var regexp;
		if (link.indexOf('@') < 0) link += '@data';
		regexp = /(#?)([^@]+)@([^:]+)(:([^:]+)(:([^:]+))?)?/;
		var tmp = regexp.exec(link);
		// Bro.log(link+'::'+tmp);
		if (tmp[1]) this.clean = true;
		var res = {
			model: tmp[2],
			target: tmp[3],
			view_xslt: (tmp[5] || tmp[2]),
			edit_xslt: tmp[7]
		};
		return res;
	},
	follow: function() {
		var lnk = this.toViews();
		var csd = new MVC.Cascade(lnk);
		var links = this.__parsed;
		var self = this;
		csd.addEvent('complete', function() {
			self.unselectInactiveLinks();
			var lnk = self.toViews();
			// Bro.log('setting actives! '+lnk.length);
			for(var x=0; x<lnk.length; x++) {
				var link = links[x];
				// Bro.log('link');
				if (!link.model) continue;
				var ajx = link.model+'@'+link.target;
				// Bro.log('Ajax:::: `'+ajx+'`...');
				if (link.view_xslt) {
					if(link.view_xslt != link.model) ajx += ':'+link.view_xslt;
				}
				if (link.edit_xslt) ajx += ':'+link.edit_xslt;
				var elem = $$('[ajax*='+ajx+']');
				// Bro.log('activating: '+elem);
				elem.addClass('active');
			}
		});
		csd.run();
	},
	toViews: function() {
		var parsed = this.__parsed;
		var views = new Array();
		for(var x=0;x<parsed.length;x++) {
			views.push(this.toView(x));
		}
		return views;
	},
	toView: function(item) {
		if (!item) item = 0;
		var act = this.__parsed[item];
		var mod = new MVC.Model(this.expandState(act.model));
		var view = new MVC.View(this.expandXSLT(act.view_xslt), {model:mod, target:act.target});
		//view.hSrc = 'usr/js/binds/'+act.model+'.js';
		return view;
	},
	expandState: function(state) {
		var app = 'gui';
		if (state.match(/\./)) {
			var st = state.split('.');
			app = st[0];
			state = st[1];
		}
			
		return 'index.php?query_id='+Xendri.nextQueryID()+'&query_type=XML&application='+app+'&state='+state;
	},
	expandXSLT: function(xslt) {
		return 'index.php?query_id='+Xendri.nextQueryID()+'	&query_type=XSLT&file='+xslt;
	}, 
	unselectInactiveLinks: function() {
		var anch = Xendri.Location.getAnchor(document.location);
		// Bro.log('Active Anchor: '+anch);
		var acts = $$('.active'); 
		for (var x=0; x<acts.length; x++) {
			var elem = acts[x];
			var ajx = elem.getAttribute('ajax').replace(/#/g, '');
			if (!(anch.match(ajx))) {
				elem.removeClass('active');
			}
		}
	},
	toString: function() {
		var ret = '';
		if (this.clean) ret = '#';
		for(var x=0; x<this.__parsed.length; x++) {
			var el = this.__parsed[x];
			var ln = '';
			if (el.model) ln += el.model;
			if (el.target) ln+= '@'+el.target;
			if (el.view_xslt != el.model) ln+=':'+el.view_xslt;
			if (el.edit_xslt) ln+=':'+el.edit_xslt;
			if (ln != '') {
				if (x > 0) ret += '/';
				ret += ln;
			}
		}
		return ret;
	}, 
	removeTarget: function (ly) {
		var links = this.__parsed;
		for(var x=0; x<links.length; x++) {
			if (links[x].target != ly) continue;
			// Bro.log('target '+ly+' found at position'+x);
			links[x] = {};
		}
	}, 
	contains: function(s) {
		for(var i=0; i<this.__parsed.length; i++) {
			var m = this.__parsed[i];
			
			if (m.model == s.model && m.target == s.target && m.view_xslt == s.view_xslt) return true;
		}
	}, 
	expell: function(link) {
		var p = new Array();
		for(var i=0; i<this.__parsed.length; i++) {
			var m = this.__parsed[i];
			if (!link.contains(m)) p.push(m);
		}
		this.__parsed = p;
		this.url = ''+this.toString();
	}
})